//
//  CGQshebeiInfoEditCell.swift
//  SheYangBigData
//
//  Created by 叁拾叁 on 2021/4/8.
//  Copyright © 2021 叁拾叁. All rights reserved.
//


import UIKit


class CGQshebeiInfoEditCell: SYBaseCell {
    
    var inputClosure: ((String) -> Void)?
    
    let title: UILabel = {
        let v:UILabel = UILabel()
        v.textColor = UIColor(hex: 0x333333)
        v.text = "设备离线"
        v.font = 14.autoBoldFontSize()

        return v
    }()
    
    let content: UITextField = {
        let v:UITextField = UITextField()
        v.textColor = UIColor(hex: 0x666666)
        v.font = 15.autoFontSize()
        v.text = "设备离线"
        v.isEnabled = false
        
        return v
    }()
    
    let duohang: UILabel = {
        let v:UILabel = UILabel()
        v.textColor = UIColor(hex: 0x666666)
        v.font = 15.autoFontSize()
        v.numberOfLines = 0
        return v
    }()
    
    let arrowImg: UIImageView = {
        
        let v:UIImageView = UIImageView()
        v.sy_name("my_arrow")
        v.isHidden = true
        
        return v
    }()
    
    let textView: UITextView = {
        let v:UITextView = UITextView()
        v.textColor = UIColor(hex: 0x666666)
        v.font = 15.autoFontSize()
        v.text = "设备离线"
        return v
    }()
    
    var sepView: UIView = {
        let v = UIView()
        v.backgroundColor = Constant.bgViewColor
        return v
    }()
    var bgView: UIView = {
        let v = UIView()
        v.backgroundColor = .white
        v.layer.masksToBounds = true
        v.layer.cornerRadius = 5.autoWidth()
        return v
    }()
    
    public func configureView(model: CGQshebeiinfoModel, row: Int) {
        
        content.textColor = UIColor(hex: 0x666666)
        content.isHidden = false
        duohang.isHidden = true
        switch row {
        case 0:

            title.text = "所属项目"
            duohang.text = model.appName
            content.placeholder = "请选择所属项目"
            content.isEnabled = false
            arrowImg.isHidden = true
            content.isHidden = true
            duohang.isHidden = false
            
            content.snp.remakeConstraints { (make) in
                make.top.equalToSuperview().offset(autoWidth(65))
                make.left.equalToSuperview().offset(autoWidth(10))
                make.right.equalToSuperview().offset(autoWidth(-35.0))
    //            make.height.greaterThanOrEqualTo(15.autoWidth())
                
            }
            duohang.snp.remakeConstraints { (make) in
                make.top.equalToSuperview().offset(autoWidth(65))
                make.left.equalToSuperview().offset(autoWidth(10))
                make.right.equalToSuperview().offset(autoWidth(-35.0))
    //            make.height.greaterThanOrEqualTo(15.autoWidth())
                make.bottom.equalToSuperview().offset(-15.autoWidth())
            }
            break
        case 1:
            title.text = "采集端口"
            content.text = model.portName
            arrowImg.isHidden = false
            content.placeholder = "请选择采集端口"
            content.isEnabled = false
            
            break
        case 2:
            title.text = "解析协议"
            content.text = model.protocolName
            self.backgroundColor = UIColor(hex: 0xf0f0f0)
            arrowImg.isHidden = false
            content.isEnabled = true
            break
        case 3:
            title.text = "设备类型"
            content.text = model.protocolDeviceTypeName
            arrowImg.isHidden = false
            content.isEnabled = true
            content.placeholder = " 请选择设备类型"
            
            break
        case 4:
            title.text = "设备编号"
            content.text = model.deviceId
            content.placeholder = "请输入设备编号"
            content.isEnabled = true
            arrowImg.isHidden = true
            self.bgView.backgroundColor = UIColor(hex: 0xf0f0f0)

            break
        case 5:
            title.text = "设备名称"
            content.text = model.deviceName
            content.placeholder = "请输入设备名称"
            content.isEnabled = true
            arrowImg.isHidden = true

            
            break
            
            
        case 6:
            
            
            if model.isOnline == 1 {
                content.text = "在线"
                content.textColor = UIColor(hex: 0x666666)
            }else {
                content.text = "离线"
                content.textColor = UIColor(hex: 0xDE5D5C)
            }
            title.text = "状态"
            content.isEnabled = false
            arrowImg.isHidden = true
            
            break
        case 7:
            
            title.text = "设备位置"
            content.text = model.deviceLocation
            content.isEnabled = true
            content.placeholder = "请输入设备位置"
            arrowImg.isHidden = true
            break
        case 8:
            title.text = "设备描述"
            content.text = model.deviceComment
            content.isEnabled = true
            content.placeholder = "请输入设备描述"
            arrowImg.isHidden = true

            break
            
        default:
            break
        }
        
        
    }
    
    override func initViews() {
        
        content.rx.controlEvent([.editingDidEnd]) //状态可以组合
            .asObservable()
            .subscribe(onNext: { _ in
                print("结束编辑编辑内容!")
                self.passParamsClosure?(self.content.text as Any)
                
            }).disposed(by: disposeBag)
        
        
        //当文本框内容改变时，将内容输出到控制台上
        textView.rx.text.orEmpty.asObservable()
            .subscribe(onNext: {
                print("您输入的是：\($0)")
                if $0 == "" {
                    //                    self.moren.isHidden = false
                } else {
                    //                    self.moren.isHidden = true
                }
                
            })
            .disposed(by: disposeBag)
        
        //开始编辑响应
        textView.rx.didBeginEditing
            .subscribe(onNext: {
                print("开始编辑")
                //                self.choiceView.isHidden = true
            })
            .disposed(by: disposeBag)
        //结束编辑响应
        textView.rx.didEndEditing
            .subscribe(onNext: {
                print("结束编辑")
                //                self.contentStr = self.textView.text
                
                self.passParamsClosure?(self.textView.text as Any)
            })
            .disposed(by: disposeBag)
        
        self.backgroundColor = Constant.bgViewColor
        contentView.addSubview(bgView)
        contentView.addSubview(sepView)
        bgView.addSubview(title)
        bgView.addSubview(content)
        bgView.addSubview(duohang)
        bgView.addSubview(lineView)
        bgView.addSubview(arrowImg)
        
 

        sepView.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.height.equalTo(10.autoWidth())
            make.top.equalToSuperview()
        }
        bgView.snp.makeConstraints { (make) in
            
            make.left.equalToSuperview().offset(15.autoWidth())
            make.right.equalToSuperview().offset(-15.autoWidth())
            make.bottom.equalToSuperview().offset(0)
            make.top.equalTo(sepView.snp.bottom).offset(0)
        }
        title.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(autoWidth(23))
            make.left.equalToSuperview().offset(autoWidth(10))
            make.right.equalToSuperview().offset(autoWidth(-10.0))
            make.height.equalTo(14.autoWidth())
        }
        
        arrowImg.snp.makeConstraints { (make) in
            
            make.centerY.equalTo(content.snp.centerY).offset(0)
            make.right.equalToSuperview().offset(autoWidth(-15))
            make.height.equalTo(16.autoWidth())
            make.width.equalTo(16.autoWidth())
            
        }
        content.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(autoWidth(65))
            make.left.equalToSuperview().offset(autoWidth(10))
            make.right.equalToSuperview().offset(autoWidth(-35.0))
//            make.height.greaterThanOrEqualTo(15.autoWidth())
            make.bottom.equalToSuperview().offset(-15.autoWidth())
        }
        
        duohang.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(autoWidth(65))
            make.left.equalToSuperview().offset(autoWidth(10))
            make.right.equalToSuperview().offset(autoWidth(-35.0))
//            make.height.greaterThanOrEqualTo(15.autoWidth())
            
        }
        
        lineView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(autoWidth(50))
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.height.equalTo(0.5.autoWidth())
        }
    }
}
